package edu.dayu.controller;

import com.github.pagehelper.PageInfo;
import edu.dayu.pojo.Blog;
import edu.dayu.pojo.Comment;
import edu.dayu.pojo.Tag;
import edu.dayu.pojo.Type;
import edu.dayu.service.BlogService;
import edu.dayu.service.CommentService;
import edu.dayu.service.TagService;
import edu.dayu.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class MockController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired
    private CommentService commentService;

    @GetMapping({"/","/index"})
    public String toIndex(Blog blog, Model model){
        PageInfo<Blog> blogPageInfo = blogService.listBlog(blog);
        model.addAttribute("blogs",blogPageInfo);
        int i = blogService.countBlog();
        model.addAttribute("countBlog",i);
        //得到前三个最多的分类
        List<Type> typeList = typeService.countTopThree();
        model.addAttribute("types",typeList);
        List<Tag> tags = tagService.countTagTopThree();
        model.addAttribute("tags",tags);
        return "index";
    }

    /**
     * 前端分类页面
     */
    @GetMapping("/toType")
    public String toType(Blog blog,Model model){
        Integer integer = typeService.countType();
        model.addAttribute("countType",integer);
        List<Type> typeList = typeService.countTopAll();
        model.addAttribute("types",typeList);
        if (blog.getTypeid()==0){
            blog.setTypeid(typeList.get(0).getId());
        }
        PageInfo<Blog> blogPageInfo = blogService.listBlog(blog);
        model.addAttribute("blogs",blogPageInfo);
        return "types";
    }

    /**
     * 前端分类页面
     * 局部刷新
     */
    @PostMapping("/TypeSearch")
    public String TypeSearch(Blog blog,Integer typeid,Integer num,Model model){
        blog.setTypeid(typeid);
        blog.setPageNum(num);
        PageInfo<Blog> blogPageInfo = blogService.listBlog(blog);
        model.addAttribute("blogs",blogPageInfo);
        return "types::typeslimit";
    }


    /**
     * 前端标签页面
     */
    @GetMapping("/toTag")
    public String toTag(Blog blog ,Model model){
        Integer integer = tagService.countTagAll();
        model.addAttribute("countTag",integer);
        List<Tag> tags = tagService.countTagTopAll();
        model.addAttribute("tags",tags);
        if (blog.getTagid()==0){
            blog.setTagid(tags.get(0).getId());
        }
        PageInfo<Blog> blogPageInfo = blogService.listBlog(blog);
        model.addAttribute("blogs",blogPageInfo);
        return "tags";
    }

    /**
     * 前端标签页面
     * 局部刷新
     */
    @PostMapping("/TagSearch")
    public String TagSearch(Blog blog,Integer tagid,Integer num,Model model){
        blog.setTagid(tagid);
        blog.setPageNum(num);
        PageInfo<Blog> blogPageInfo = blogService.listBlog(blog);
        model.addAttribute("blogs",blogPageInfo);
        return "tags::typeslimit";
    }

    /**
     * 博客详情
     * 增加浏览量
     * 查看评论信息
     */
    @GetMapping("toBlog")
    public String toBlog(Blog blog ,Model model){
        //初始化博客内容
        Blog blog1 = blogService.getBlog(blog);
        model.addAttribute("blog",blog1);
        //增加浏览量
        blogService.updateBlogViews(blog);
        //初始化评论
        Comment comment = new Comment();
        comment.setBlog_id(blog.getId());
        comment.setParent_id(-1);
        List<Comment> comments = commentService.listComment(comment);
        model.addAttribute("comments",comments);
        return "blog";
    }

    /**
     * 评论增加
     */
    @PostMapping("/commentSearch")
    public String commentSearch(Comment comment,Model model){
        commentService.insertComment(comment);
        comment.setParent_id(-1);
        List<Comment> comments = commentService.listComment(comment);
        model.addAttribute("comments",comments);
        return "blog :: commentList";
    }


    /**
     * 归档页面
     */
    @GetMapping("/archives")
    public String archives(Model model){
        Map<String, List<Blog>> stringListMap = blogService.listBlogByYear();
        model.addAttribute("archives",stringListMap);
        model.addAttribute("countBlog",blogService.countBlog());
        return "archives";
    }


    /**
     *关于我页面实现
     */
    @GetMapping("/about")
    public String about(){
        return "about";
    }
}
